package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.LuckKeyDao;
import com.jbp.exception.RRException;
import com.jbp.model.*;
import com.jbp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 钥匙(LuckKey)表服务实现类
 * @author cc
 * @since 2021-04-29 17:54:46
 */
@Service
public class LuckKeyServiceImpl extends ServiceImpl<LuckKeyDao, LuckKey> implements LuckKeyService {
    @Autowired
    private WalletService walletService;
    @Autowired
    private LuckKeyBuyService luckKeyBuyService;
    @Autowired
    private LuckKeyRecordService luckKeyRecordService;
    @Autowired
    private WalletRecordService walletRecordService;

    @Autowired
    private RankProportionService rankProportionService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<LuckKey> queryWrapper = new QueryWrapper<>();
        if (params.get("id") != null){
            queryWrapper.like("id",params.get("id"));
        }
        IPage<LuckKey> page = this.page(
                new Query<LuckKey>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    //数据更新，加乐观锁
    @Override
    public void updateWithLock(LuckKey luckKey) {
        Integer result = getBaseMapper().updateWithLock(luckKey);
        if (null == result || result < 1) {
            throw new RRException("服务器忙");
        }
    }

    @Override
    @Transactional
    public void buy(Integer userId, LuckKey req) {
        //检查余额
        Wallet wallet = walletService.getByUserId(userId);
        if (wallet == null) {
            throw new RRException("用户数据异常");
        }
        if (req.getNum().intValue()<1) {
            throw new RRException("金额错误");
        }

        int xinzuan = req.getNum()*25;
        if (wallet.getXinzuan().intValue() <  xinzuan) {
            throw new RRException("余额不足");
        }

        LuckKey luckKey = getBuyUserId(userId);
        if (luckKey==null) {
            luckKey = new LuckKey();
            luckKey.setUserId(userId);
            luckKey.setNum(0);
            luckKey.setVersion(1);
            save(luckKey);
        }

        //增加购买记录
        LuckKeyBuy luckKeyBuy = new LuckKeyBuy();
        luckKeyBuy.setUserId(userId);
        luckKeyBuy.setNum(req.getNum());
        luckKeyBuy.setXinzuan(BigDecimal.valueOf(xinzuan));
        luckKeyBuyService.save(luckKeyBuy);

        //增加钥匙变动记录
        LuckKeyRecord luckKeyRecord = new LuckKeyRecord();
        luckKeyRecord.setUserId(userId);
        luckKeyRecord.setAddType("add");
        luckKeyRecord.setOperationType("buy");
        luckKeyRecord.setBeforeBalance(luckKey.getNum());
        luckKeyRecord.setChangeBalance(req.getNum());
        luckKeyRecord.setAfterBalance(luckKey.getNum()+req.getNum());
        luckKeyRecordService.save(luckKeyRecord);

        //增加心钻变动记录
        WalletRecord walletRecordXinZuan = new WalletRecord();
        walletRecordXinZuan.setUserId(userId);
        walletRecordXinZuan.setAddType("sub");
        walletRecordXinZuan.setCoinType("xinzuan");
        walletRecordXinZuan.setOperationType("buykey");
        walletRecordXinZuan.setBeforeBalance(wallet.getXinzuan());
        walletRecordXinZuan.setChangeBalance(BigDecimal.valueOf(xinzuan));
        walletRecordXinZuan.setAfterBalance(wallet.getXinzuan().subtract(BigDecimal.valueOf(xinzuan)));
        walletRecordService.save(walletRecordXinZuan);

        //百万奖池金额处理
        rankProportionService.addPrizePoolAmount(walletRecordXinZuan.getChangeBalance());

        //修改钱包余额
        wallet.setXinzuan(wallet.getXinzuan().subtract(BigDecimal.valueOf(xinzuan)));
        walletService.updateWithLock(wallet);

        //修改钥匙余额
        luckKey.setNum(luckKey.getNum()+req.getNum());
        updateWithLock(luckKey);
    }

    @Override
    public LuckKey getBuyUserId(Integer userId) {
        return getOne(new QueryWrapper<LuckKey>().eq("user_id", userId));
    }
}
